package com.neu.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.neu.pojo.Room;
import com.neu.pojo.Worker;
import com.neu.service.impl.RoomService;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JButton;

public class AddRoomUI extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	
	private RoomService roomService = RoomService.getInstance();
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddRoomUI frame = new AddRoomUI(new Worker());
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddRoomUI(Worker w) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 311);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("房间号");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 22));
		lblNewLabel.setBounds(47, 31, 133, 26);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("房间类型");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 22));
		lblNewLabel_1.setBounds(47, 74, 133, 26);
		contentPane.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("可容纳人数");
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 22));
		lblNewLabel_2.setBounds(47, 116, 133, 26);
		contentPane.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("物业负责人");
		lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel_3.setFont(new Font("微软雅黑", Font.PLAIN, 22));
		lblNewLabel_3.setBounds(47, 162, 133, 26);
		contentPane.add(lblNewLabel_3);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(190, 74, 167, 35);
		contentPane.add(textField_1);
		
		textField = new JTextField();
		textField.setColumns(10);
		textField.setBounds(190, 31, 167, 35);
		contentPane.add(textField);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(190, 117, 167, 35);
		contentPane.add(textField_2);
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		textField_3.setBounds(190, 162, 167, 35);
		contentPane.add(textField_3);
		
		JButton btnNewButton = new JButton("确定");
		btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		btnNewButton.setBounds(83, 224, 97, 35);
		contentPane.add(btnNewButton);
		btnNewButton.addActionListener((e)->{
			if(textField.getText().length() != 0&&textField_1.getText().length() != 0&&textField_2.getText().length() != 0&&textField_3.getText().length() != 0) {
				Worker worker = new Worker();
				worker.setName(textField_3.getText());
				if(this.roomService.add(new Room(textField.getText(), textField_1.getText(), Integer.parseInt(textField_2.getText()), 0, worker))) {
					JOptionPane.showMessageDialog(null, "房间创建成功");
				}else {
					JOptionPane.showMessageDialog(null, "房间创建失败");
				}
				new RoomUI(w).setVisible(true);
				setVisible(false);
			}else {
				JOptionPane.showMessageDialog(null, "请输入内容");
			}
		});
		
		JButton btnNewButton_1 = new JButton("返回");
		btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		btnNewButton_1.setBounds(231, 224, 97, 35);
		contentPane.add(btnNewButton_1);
		btnNewButton_1.addActionListener((e)->{
			new RoomUI(w).setVisible(true);
			setVisible(false);
		});
		
		//窗口居中
		setLocationRelativeTo(null);
	}
}
